//  Created by <Omar MALAGOUEN>

//  Copyright 2011 All rights reserved.
//

/*
 * Various utilities for command line tools
 * Copyright (c) 2000-2010 Omar Malagouen
 *
 * This file is part of Kakola.
 *
 * Kakola is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Kakola is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Kakola; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "cprofile.h"



CProfile::CProfile()

{
    program[0] = "ffmpeg";
    group = "Other";
    popupDone = "false";
    coef = 100;
}


bool CProfile::programContainString(QString str)
{
 for(int i = 0; i <= 99 ; i++)
    {
        if (this->program[i].contains(str))
            return true;
    }
 return false;
}


void CProfile::getCProfileFromFile(QString filename)
{
    QObject *obj = new QObject();

    QSettings *settingsProfile = new QSettings(filename,QSettings::IniFormat,obj);  
    QSettings *confFile = new QSettings(QSettings::NativeFormat,QSettings::UserScope,QCoreApplication::organizationName(),QCoreApplication::applicationName() );

    if ( ! QFileInfo(filename).exists() ){
        qDebug("Error to load profile %s file does not exist",filename.toLatin1().data());
        filename = QString(getenv("HOME")) + getProfileDir() + confFile->value("defaultProfile").toString();
        filename = QDir::cleanPath(filename);
        qDebug("Use default profile instead : %s",filename.toLatin1().data());
    }

    this->description = settingsProfile->value("description").toString();
    this->extension = settingsProfile->value("extension").toString();
    this->outputDir = settingsProfile->value("outputDir",confFile->value("destinationDirectory").toString()).toString();
    this->outputDir.replace("\\","/");
    this->prefix = settingsProfile->value("prefix").toString();
    this->popupDone = settingsProfile->value("popupDone").toString();
    this->popupDoneMsg = settingsProfile->value("popupDoneMsg","Encoding ended successfully !").toString();
    this->suffix = settingsProfile->value("popupDone").toString();
    this->program[0] = settingsProfile->value("program[0]","ffmpeg").toString();
    this->group = settingsProfile->value("group","Other").toString();
    bool b;
    this->coef = settingsProfile->value("coef","100").toInt(&b);
    this->filename = QFileInfo(filename).fileName();
    this->dirname = QFileInfo(filename).canonicalPath();

    this->filename = QDir::cleanPath(this->filename);
    this->dirname = QDir::cleanPath(this->dirname);

    this->listCommand.clear();
    QString str = "cmd[0]";
    QString *the_command = new QString(settingsProfile->value(str,"ERROR_NO_COMMAND_DEFINED").toString());
    qDebug("getCProfileFromFile %s command = *%s*",filename.toLatin1().data(),
           the_command->toLatin1().data());



    this->listCommand.append(the_command);

    int i = 1;
    str = "cmd[1]";
    while(settingsProfile->value(str).toString() != "")
    {
        this->listCommand.append(new QString(settingsProfile->value(str).toString()));
        i++;
        str = "cmd[" + QString::number(i,10) + "]";
    }

    if (this->outputDir == "")
        this->outputDir = confFile->value("destinationDirectory").toString();
    if (this->program[0] == "")
        this->program[0] = "ffmpeg";

    if (this->group == "")
        this->group = "Other";
}










